package com.bsoft.gol.hbs.healthinfo;


import com.bsoft.gol.hbs.bean.enums.ArticleStatusEnum;
import com.bsoft.gol.hbs.bean.exception.ServiceException;
import com.bsoft.gol.hbs.dao.HealthInfoDAO;
import com.bsoft.gol.hbs.healthinfo.state.EditedState;
import com.bsoft.gol.hbs.healthinfo.state.NotSupportState;
import com.bsoft.gol.hbs.healthinfo.state.ReleasedState;
import com.bsoft.gol.hbs.healthinfo.state.SubmittedState;
import com.bsoft.gol.hbs.utils.CommonUtils;
import ctd.account.UserRoleToken;
import ctd.util.AppContextHolder;
import hcn.base.healthinfo.HealthInfo;
import org.atag.util.bbp.RoleUtils;
import pcn.enums.RoleIdEnum;

import java.util.Date;

/**
 * 健康资讯文章流转处理器
 */
public class HealthInfoHandler {

    private StateContext context;

    public HealthInfoHandler(int state, String articleId) {
        this.context = new StateContext();
        context.setState(choose(state));
        context.setArticleId(articleId);
    }

    private State choose(int state) {
        ArticleStatusEnum stateEnum = ArticleStatusEnum.getByState(state);
        switch (stateEnum) {
            case EDITED:
                return new EditedState();
            case SUBMITTED:
                return new SubmittedState();
            case RELEASED:
                return new ReleasedState();
            default:
                return new NotSupportState();
        }
    }
    /**
     * 提交
     */
    public void submit() {
        context.next();
        HealthInfo update = new HealthInfo();
        update.setSubmitTime(new Date());
        saveState(context, update);
    }

    /**
     * 发布
     */
    public void release() {
        context.next();
        HealthInfo update = new HealthInfo();
        update.setReleaseTime(new Date());
        update.setReleaseUserId(UserRoleToken.getCurrent().getUserId());
        saveState(context, update);
    }

    /**
     * 退回
     */
    public void reject() {
        context.prev();
        saveState(context);
    }

    /**
     * 撤回
     */
    public void withdraw() {
        context.prev();
        saveState(context);
    }

    /**
     * 取消
     */
    public void cancel() {
        context.prev();
        saveState(context);
    }

    /**
     * 管理员撤回
     */
    public void withdrawByAdmin() {
        context.prev();
        saveState(context);
    }

    /**
     * 保存状态
     */
    private void saveState(StateContext context, HealthInfo update) {

        if (RoleIdEnum.PATIENT.title().equals(RoleUtils.getCurrentCommonRole())) {
            throw new ServiceException("权限不足");
        }
        AbstractState state = (AbstractState) context.getState();
        String articleId = context.getArticleId();
        HealthInfoDAO healthInfoDAO = (HealthInfoDAO) AppContextHolder.getBean("healthInfoDAO");
        if (CommonUtils.isEmpty(update)) {
            update = new HealthInfo();
        }
        update.setArticleId(articleId);
        update.setArticleStatus(state.getState());
        update.setGmtModified(new Date());
        healthInfoDAO.updateSelective(update);
    }

    private void saveState(StateContext context) {
        saveState(context, null);
    }
}
